﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Threading;
using System.IO;

namespace eventr.Web.Service
{
    public class ImageViewManager
    {
        static ReaderWriterLock locker = new ReaderWriterLock();

        public ImageViewManager()
        {

        }

        static public void SaveImageView(int imageId)
        {
            locker.AcquireWriterLock(20000);

            try
            {
                FileInfo fCheck = new FileInfo(imageDataFile);

                XDocument doc;


                if (!fCheck.Exists)
                {
                    doc = XDocument.Parse("<root/>");
                }
                else
                {
                    doc = XDocument.Load(imageDataFile);
                }


                XElement ele = doc.Root.Elements("Image").SingleOrDefault(p => p.Attribute("id").Value == imageId.ToString());

                if (ele != null)
                {

                    int count = Convert.ToInt32(ele.Attribute("count").Value);

                    count++;

                    ele.Attribute("count").Value = count.ToString();

                    doc.Save(imageDataFile);
                }

            }
            finally
            {
                locker.ReleaseWriterLock();
            }

        }

        static public List<ImageMetaData> GetImageViews()
        {
            List<ImageMetaData> list = new List<ImageMetaData>();
            locker.AcquireReaderLock(20000);
            try
            {

                FileInfo fCheck = new FileInfo(imageDataFile);



                if (fCheck.Exists)
                {

                    XDocument doc = XDocument.Load(imageDataFile);

                    doc.Root.Elements("Image").ToList<XElement>().ForEach(
                            p =>
                            {
                                //note the id must be one less than the zorder from 
                                //the metadata file... 
                                int id = Convert.ToInt32(p.Attribute("id").Value);
                                string url = p.Attribute("url").Value;
                                string tag = p.Attribute("tag").Value;
                                string title = p.Attribute("title").Value;
                                string blurb = p.Value.Trim();
                                string video = "";
                                if (p.Attribute("video") != null)
                                {
                                    video = p.Attribute("video").Value;
                                }
                                //map this id back to the original collection
                                ImageMetaData item = new ImageMetaData(id, tag)
                                {
                                    Blurb = blurb,
                                    Title = title,
                                    Url = url,
                                    Count = Convert.ToInt32(p.Attribute("count").Value),
                                    Video = video
                                };
                                list.Add(item);

                            }
                        );



                }
            }
            finally
            {
                locker.ReleaseReaderLock();
            }
            return list;

        }

        static string imageDataFile
        {
            get
            {
                string fileName = string.Format("{0}/ImageData.xml", AppDomain.CurrentDomain.BaseDirectory);
                return fileName;
            }
        }
    }

    public class ImageMetaData
    {
        int id;
        string tag;
        string title;
        string blurb;
        string url;
        string video;

        public ImageMetaData()
        {

        }

        public ImageMetaData(int id, string tag)
        {
            this.id = id;
            this.tag = tag;
        }

        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public int Count
        {
            get;
            set;
        }

        public string Tag
        {
            get
            {
                return tag;
            }
            set
            {
                tag = value;
            }
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        public string Blurb
        {
            get
            {
                return blurb;
            }
            set
            {
                blurb = value;
            }
        }

        public string Video
        {
            get
            {
                return video;
            }
            set
            {
                video = value;
            }
        }

        public string Url
        {
            get
            {
                return url;
            }
            set
            {
                url = value;
            }
        }

    }

}
